#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 4e5 + 5;

int n, m;
int f[N];
void init(int n) { rep(i, 1, n) f[i] = i; }
int find(int x) { return f[x] == x ? x : f[x] = find(f[x]); }

void merge(int x, int y) {
  int fx = find(x), fy = find(y);
  f[fy] = fx;
}
bool same(int x, int y) { return find(x) == find(y); }

void solve() {
  cin >> n;
  map<int, int> mp;
  int tot = 0;
  init(n * 2);
  vector<pii> diff;
  rep(i, 1, n) {
    int x, y, z;
    cin >> x >> y >> z;
    if (!mp.count(x)) mp[x] = ++tot;
    if (!mp.count(y)) mp[y] = ++tot;
    x = mp[x], y = mp[y];
    if (z)
      merge(x, y);
    else
      diff.push_back(pii(x, y));
  }
  bool res = true;
  for (pii p : diff) {
    if (same(p.first, p.second)) res = false;
  }
  cout << (res ? "YES" : "NO") << endl;
}

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}